#!/usr/bin/env bash
set -euo pipefail

# 定位到脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

# 路径设置
EVAL_PY="$SCRIPT_DIR/llm_eval.py"
CATEGORIZE_PY="$SCRIPT_DIR/categorize_and_report.py"
INPUT_FILE="$PROJECT_ROOT/data/processed/units_prs_merged.jsonl"
EVAL_OUT_DIR="$PROJECT_ROOT/data/label_data"
LABEL_LOG_DIR="$PROJECT_ROOT/logs/label_log"

mkdir -p "$EVAL_OUT_DIR" "$LABEL_LOG_DIR"

echo "[1/2] 运行评估脚本（两步：有效性 -> 多轮分类投票）..."
API_KEY="${API_KEY:-EMPTY}" \
BASE_URL="${BASE_URL:-http://localhost:15070}" \
MODEL_NAME="${MODEL_NAME:-/remote-home1/fyyuan/tool_research/models/QwQ-32B}" \
MAX_CONCURRENCY="${MAX_CONCURRENCY:-40}" \
NUM_CLASSIFY_RUNS="${NUM_CLASSIFY_RUNS:-5}" \
INPUT_FILE="$INPUT_FILE" \
OUTPUT_DIR="$EVAL_OUT_DIR" \
LOG_DIR="$LABEL_LOG_DIR" \
python3 "$EVAL_PY"

echo "定位最新的评估结果 JSONL..."
LATEST_JSONL=$(ls -1t "$EVAL_OUT_DIR"/evaluation_results_*.jsonl | head -n 1)
if [[ -z "${LATEST_JSONL:-}" ]]; then
  echo "未找到评估输出文件，请检查评估是否成功。" >&2
  exit 1
fi
echo "使用结果文件: $LATEST_JSONL"

echo "[2/2] 运行数据分类与统计报告生成（导出三类：auto_pass/weak/confusing）..."
python3 "$CATEGORIZE_PY" "$LATEST_JSONL" --label-data-dir "$EVAL_OUT_DIR" --log-dir "$LABEL_LOG_DIR" --save-valid --save-weak --save-confusing

echo "完成。"
echo "  - 评估结果和三类数据已保存到: $EVAL_OUT_DIR"
echo "  - 统计报告已保存到: $LABEL_LOG_DIR"

